﻿function startNotifications() {
    // Save the pagetitle
    pagetitle = $(document).attr('title');
    
    // Initial poll
    poll(); 

    // Set poll timer
    setInterval(function(){ 
        poll(); 
    }, interval);

    $('#notification-icon').bind('click', function() {
        getNotificationList(userid);
    });
}
function poll() {
    countNotifications(userid);
}

function setNotificationCounter(number) {
    var counter = $('#notification-counter');
    var icon = $('#notification-icon i');
    var title = pagetitle;
    if(parseInt(number) == 0) {
        $(counter).hide();
        $(icon).removeClass('icon-white');
    } else {
        $(counter).show();
        $(icon).addClass('icon-white');
        title = '(' + number + ') ' + pagetitle;
    }
    $(counter).html(number);
    $(document).attr('title', title);
    return false;
}
    
function countNotifications(userid) {
    var base_url = count_url;
    //var url = base_url + "?parametro=" + userid;    
    var url = base_url;
    $.ajax({
        url: url,
        data: "{ parametro:" + userid + "}",
        dataType: 'json',
        contentType: "application/json; charset=utf-8",
        type: 'post',
        cache: false
    }).done(function(result) {
        setNotificationCounter(result.d);
    });
    return false;
}
    
function setNotificationItems(content) {
    $('.notification-icon .notification-item').remove();
    $('.notification-icon .notification-empty').remove();
    var spinner = $('#notification-spinner');
    
    $(spinner).before(CriarNotificacao(content));
    $(spinner).hide();
    return false;
}

function getNotificationList(userid) {
    $('.notification-icon .notification-item').hide();
    $('.notification-icon .notification-empty').hide();
    var spinner = $('#notification-spinner');
    $(spinner).show();
    
    //var url = list_url + "/" + userid;
    var url = list_url;
    
    $.ajax({
        url: url,
        data: "{ parametro:" + userid + "}",
        dataType: 'json',
        contentType: "application/json; charset=utf-8",
        type: 'post',
        cache: false
    }).done(function(result) {
        
        setNotificationItems(result.d);
    });
    return false;
}

function CriarNotificacao(items) {
    if (items.length == 0)
        retorno =
        "<li class=\"notification-empty\">" +
            "<h4>No notifications</h4>" +
        "</li>";
    else {
        var notificacao = "<li id=\"{notificacaoId}\" class=\"notification-item {lida}\" style=\"position: relative;\">" +
            "<a id=\"{notificacaoId}\" href=\"{url}\">" +
                "<div>" +
                    "<div class=\"avatar pull-left\">" +
                        "<img src='" + eS.UrlPadrao + "recursos/upload/avatar.jpg' />" +
                    //"<?php echo $this->Html->image($data['avatar']); ?>" +
                "</div>" +
                "<div class=\"message\">" +
                    "{message}" +
                "</div>" +
                "<div class=\"timestamp\">" +
                    "{time}" +
                "</div>" +
                "<div style=\"clear: both;\"></div>" +
            "</div>" +
        "</a>" +
        "{linklida}" +
    "</li>"
        var retorno = "";
        for (var i in items) {
            retorno += notificacao
                .replace("{lida}", items[i].Visualizado ? "" : "unread")
                .replace("{url}", "")
                .replace("{linklida}", items[i].Visualizado ? "" : "<a href=\"javascript:void(0);\" onclick=\"setRead('" + items[i].Identificador + "', this, event);\" class=\"deletelink\">x</a>")
                .replace("{avatar}", items[i].UsuarioIndicadorId)
                .replace("{message}", items[i].Menssagem)
                .replace("{time}", "")
                .replace("{notificacaoId}", items[i].Identificador)
                .replace("{notificacaoId}", items[i].Identificador);
        }
    }
    return retorno;
}
function setRead(identificador, item, event) {
    var url = read_url;
    event.stopPropagation();
    $.ajax({
        url: url,
        data: "{ identificador:'" + identificador + "'}",
        dataType: 'json',
        contentType: "application/json; charset=utf-8",
        type: 'post',
        cache: false
    }).done(function (result) {
        countNotifications(userid);
        $(item).parent().removeClass("unread");
        $(item).remove();        
    });
    return false;
}